package com.shuqi.bookbag;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;

import com.shuqi.application.ShuqiApplication;
import com.shuqi.common.Config;

public class BookBagDao {
    private static BookBagDao mDao= null;
    public static BookBagDao getInstance(){
        if(mDao==null){
            mDao = new BookBagDao();
        }
        return mDao;
    }
    
    /**
     * 删除数据库中所有书包的数据
     * @return
     */
    public int deleteBookBagsByBookId(String bookId) {
        if(TextUtils.isEmpty(bookId)){
            return -1;
        }
        SDDatabase datebase = null;
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            datebase = SDDatabase.getInstance(ShuqiApplication.getContext());
            db = datebase.getWritableDatabase();
            String whereClause = "book_id = " + bookId;
            int i = db.delete("book_bag", whereClause, null);
            Log.e("BookBagDao", bookId+"删除书包库记录总数："+i);
            return i;
        } catch (Exception e) {
            Log.e("BookBagDao", e.toString());
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
            if (db != null && db.isOpen()) {
                db.close();
            }
        }
        return -1;
    }
    /***
     * 书包数据库文件是否存在
     * @return
     */
    public boolean existsDataBaseFile(){
        File file = new File(Config.BOOKBAG_PATH, SDDatabase.DATABASE_NAME);
        return file.exists();
    }
    
    /**
     * 是否有已下载的书包
     * @return
     */
    public boolean isHasDownLoadedBookBag() {
        //判断数据库文件是否存在
        if(!existsDataBaseFile()){
            return false;
        }
        SDDatabase datebase = null;
        SQLiteDatabase db = null;
        Cursor cursor = null;
        String sql = null;
        try {
            datebase = SDDatabase.getInstance(ShuqiApplication.getContext());
            db = datebase.getWritableDatabase();
            sql = "select * from book_bag where isdownloaded = 1";
            cursor = db.rawQuery(sql, null);
            Log.e("BookBagDao", "书包总数目："+cursor.getCount());
            return cursor.getCount() > 0;
        } catch (Exception e) {
            Log.e("BookBagDao", e.toString());
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
            if (db != null && db.isOpen()) {
                db.close();
            }
        }
        return false;
    }
    
    /**
     * 获取书包列表
     * 
     * @return
     */
    public List<BookBagInfo> getBookBagList() {
        if(!existsDataBaseFile()){
            return null;
        }
        SDDatabase datebase = null;
        SQLiteDatabase db = null;
        Cursor cursor = null;
        String sql = null;
        List<BookBagInfo> list = new ArrayList<BookBagInfo>();
        BookBagInfo info = null;
        try {
            datebase = SDDatabase.getInstance(ShuqiApplication.getContext());
            db = datebase.getWritableDatabase();

            sql = "select * from book_bag where isdownloaded = 1 order by id desc ";

            cursor = db.rawQuery(sql, null);

            cursor.moveToFirst();

            while (!cursor.isAfterLast()) {
                info = new BookBagInfo();

                info.setBagName(cursor.getString(cursor.getColumnIndex("bag_name")));
                info.setFileName(cursor.getString(cursor.getColumnIndex("file_name")));
                info.setBookName(cursor.getString(cursor.getColumnIndex("book_name")));
                info.setBookId(cursor.getString(cursor.getColumnIndex("book_id")));
                info.setAuthor(cursor.getString(cursor.getColumnIndex("author")));
                info.setImgUrl(cursor.getString(cursor.getColumnIndex("url")));
                info.setPackageId(cursor.getString(cursor.getColumnIndex("package_id")));

                list.add(info);
                cursor.moveToNext();
            }

        } catch (Exception e) {
            Log.e("BookBagDao", e.toString());
        } finally {

            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
            if (db != null && db.isOpen()) {
                db.close();
            }
        }
        return list;
    }

}
